package com.leador.core;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.leador.assist.DataQueueManager;
import com.leador.location.LocateHelper;
import com.leador.module.AppInfo;
import com.leador.service.LBSTraceService;
import com.leador.socket.SocketMananger;
import java.util.Deque;

/**
 * Created by lhy on 2016/9/12.
 */
public class KernelThread extends Thread{
    public  boolean  isCache = false ; //缓存是否开启
    public  boolean  isLogin = false ; // 是否登陆
    public  boolean  isLogout = false ;//是否注销登陆
    public  boolean serverEnable = false; //服务器开启和停止
    private Context context = null;
    private  KernelRunnable kernelRunnable;
    public  int packIntervalTime = 30000;
    public  int gatherInterval = 5;
    private LocateHelper locateHelper = null;
    private Handler handler;
    public LBSTraceService lbsTraceService;
    public  int stopServiceFlag = 0;
    public KernelThread(LBSTraceService lbsTraceService, boolean isLogin, boolean isCache) {
        this.context = lbsTraceService.getContext();
        this.handler = lbsTraceService.getLbsServiceHandler();
        this.locateHelper = lbsTraceService.getLocateHelper();
        this.kernelRunnable = new KernelRunnable(this,locateHelper);
        this.isCache = isCache;
        this.isLogin = isLogin;
        this.lbsTraceService = lbsTraceService;
    }

    public SocketMananger getSocketMananger() {
        return lbsTraceService.getSocketManager();
    }

    public Handler getHandler() {
        return handler;
    }

    public void setPackIntervalTime(int packIntervalTime) {
        this.packIntervalTime = packIntervalTime;
    }

    public void resetInterval(){
        int gather = locateHelper.getGatherInterval();
        int scale = packIntervalTime / gather;
        if(gather <= AppInfo.heartbeat_period*1000){
            gatherInterval = gather;
        }else if((gather / 5000 * 5000) %(AppInfo.heartbeat_period * 1000) == 0){
            gatherInterval = AppInfo.heartbeat_period;
        }else{
            gatherInterval = 5000;
        }
        packIntervalTime = gather * scale;
    }

    public void setLgoinCacheEable(boolean isLogin, boolean isCache){
        this.isLogin = isLogin;
        this.isCache = isCache;
    }

    @Override
    public void run() {
        super.run();
        //开启循环
        Looper.prepare();
        handler = new Handler();
        handler.post(kernelRunnable);
        Looper.loop();
    }

    public Context getContext() {
        return context;
    }

    public  void setLoginCaheEable(boolean isLogin, boolean isCache) {
        this.isLogin = isLogin;
        this.isCache = isCache;
    }

    public void setLogout(boolean loginEnable){
        this.isLogout = loginEnable;
    }

    //-------------------------------
    private int packCount = 1; //发包计数

    public int getPackCount() {
        return packCount;
    }

    public void setPackCount() {
        packCount++;
    }

    public void clearPackCount(){
        packCount = 0;
    }

    private int cacheCount = 1; //缓存计数

    public int getCacheCount(){
        return cacheCount;
    }

    public void setCacheCount(){
        cacheCount++;
    }

    public void clearCacheCount(){
        cacheCount =0;
    }

    private int socketCount =0;

    public int getSocketCount() {
        return socketCount;
    }

    public void setSocketCount(){
        socketCount++;
    }

    public void clearSocketCount(){
        socketCount =0;
    }

    public int heartCount =0;

    public int getHeartCount() {
        return heartCount;
    }

    public void setHeartCount(){
        heartCount ++;
    }

    public void clearHeartCount(){
        heartCount = 0;
    }

    //队列
//    public  Deque realtimeDataQueue = new LinkedList();
//    public  Deque bufferDataQueue = new LinkedList();
//
//    public static Map responseUploadTimes = new LinkedHashMap();
//    public static Map uploadingDatas = new LinkedHashMap();

    public  int getQueSize() {
        DataQueueManager dataQueueManager = DataQueueManager.getInstance();
        Deque realtimeDataQueue = dataQueueManager.getRealtimeDataQueue();
        Deque bufferDataQueue = dataQueueManager.getBufferDataQueue();
        Deque tempDataQueue = dataQueueManager.getTempDataQueue();
        return realtimeDataQueue.size() + bufferDataQueue.size() + tempDataQueue.size();
    }

    public  void reStartService() {
//        //1.最后接受时间>0  2.需要回复 3.系统时间-最后恢复时间>60 4.handler!=null;
//        if(LBSTraceService.lastReceiveTime > 0 && AppInfo.packed_data_need_response == 1 && Utils.getSystemTime() - LBSTraceService.lastReceiveTime > 60 && KernelThread.handler != null) {
//            KernelThread.handler.obtainMessage(4).sendToTarget(); //开启startService。。。。。。
//        }
    }

//    public boolean sendSocketData(KernelThread arg1) {
//        return sendProtocolData();
//    }

    /**
     * 发送协议包
     * @return
     */
//    private  boolean sendProtocolData() {
//        int var0 = Utils.getSystemTime();
//        BaseBaseExchange baseBaseExchange = new SocketExchange(this);
//        baseBaseExchange.push();
//
//        byte[] var1 = baseBaseExchange.getRequestProtocolData((short)9, var0);
//        try {
//            if(var1 != null && var1.length > 0) {
//                PackRelated.a(var0);
//                SocketDataStreamManager.writeOutputStream(var1, handler);
//                return true;
//            } else {
//                PackRelated.b();
//                return false;
//            }
//        } catch (Exception var4) {
//            return false;
//        } finally {
//            ;
//        }
//    }

    public LBSTraceService getLbsTraceService() {
        return lbsTraceService;
    }

    public  void release() {
        isLogin = false;
        isCache = false;
        DataQueueManager.getInstance().setDbHasValue(false);
    }
}
